package com.hmdp.service.impl;

import com.hmdp.dto.Result;
import com.hmdp.entity.SeckillVoucher;
import com.hmdp.entity.VoucherOrder;
import com.hmdp.mapper.SeckillVoucherMapper;
import com.hmdp.mapper.VoucherOrderMapper;
import com.hmdp.service.ISeckillVoucherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.RedisIdWorker;
import com.hmdp.utils.ThroadLocal;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;


@Service
@Slf4j
public class SeckillVoucherServiceImpl extends ServiceImpl<SeckillVoucherMapper, SeckillVoucher> implements ISeckillVoucherService {
   @Autowired
   private  SeckillVoucherMapper seckillVoucherMapper;
   @Autowired
   private VoucherOrderMapper voucherOrderMapper;
   @Autowired
   private RedisIdWorker redisIdWorker;
   @Autowired
   private RabbitTemplate rabbitTemplate;
    @Autowired
    private RedissonClient redissonClient;

    private static final DefaultRedisScript<Long> SECKILL_SCRIPT;
    static {
        SECKILL_SCRIPT = new DefaultRedisScript<>();
        SECKILL_SCRIPT.setLocation(new ClassPathResource("seckill.lua"));
        SECKILL_SCRIPT.setResultType(Long.class);
    }

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Override
    @Transactional
    public Result seckkillVoucher(Long voucherId) {
        Long userId = ThroadLocal.get().getId();
        log.info("userId:{}",userId);
        log.info("voucherId:{}",voucherId);
        //执行lua脚本
       Long result =  stringRedisTemplate.execute(
                SECKILL_SCRIPT,
                Collections.emptyList(),
                voucherId.toString(),
                userId.toString()
        );
        //得到返回结果
        int r = result.intValue();
        if(r != 0){
             return Result.fail(r == 1 ? "库存不足":"不能重复下单");
        }
        Long orderId = redisIdWorker.nextId("order");
        VoucherOrder voucherOrder = new VoucherOrder();
        voucherOrder.setId(orderId);
        voucherOrder.setUserId(userId);
        voucherOrder.setVoucherId(voucherId);
        voucherOrder.setStatus(1);
        voucherOrder.setPayType(1);
        voucherOrder.setCreateTime(LocalDateTime.now());
        voucherOrder.setUpdateTime(LocalDateTime.now());
        voucherOrder.setPayTime(LocalDateTime.now());

        SeckillVoucher seckillVoucher = new SeckillVoucher();
        seckillVoucher.setVoucherId(voucherId);
        seckillVoucher.setStock(seckillVoucherMapper.SelectByid(voucherId).getStock());
        //异步下单
        rabbitTemplate.convertAndSend("order.exchange","order.queue",voucherOrder);
       //异步更新库存
       rabbitTemplate.convertAndSend("stock.exchange","stock.queue",seckillVoucher);


       return Result.ok(orderId);
    }
}
